Skip to content

DatabaseTargetTests - Add coverage for DbConnectionFactory constructor#67

Merged
snakefoot merged 1 commit into
NLog:masterfrom
JohnVerheij:tests/databasetarget-factory-ctor-coverage
May 14, 2026
Merged

DatabaseTargetTests - Add coverage for DbConnectionFactory constructor#67
snakefoot merged 1 commit into
NLog:masterfrom
JohnVerheij:tests/databasetarget-factory-ctor-coverage

Conversation

@JohnVerheij
Copy link
Copy Markdown
Contributor

@JohnVerheij JohnVerheij commented Apr 30, 2026

Introduces test coverage for the DatabaseTarget(Func<IDbConnection>) constructor. Previously covered indirectly through existing tests, this brings parity with legacy DBProvider constructor tests. Four new test cases added covering null factories, null returns, exceptions, and parameterized writes. All 219 tests pass on net8.0 with no production code modifications.

@snakefoot
Copy link
Copy Markdown
Contributor

Thank you for the contribution. Since there already multiple tests that verifies that _dbConnectionFactory works when called correctly, then I think only these tests are interesting:

  • DbConnectionFactoryNullTest - Might not compile if enabling nullable for the unit-test-project.
  • DbConnectionFactoryReturnsNullTest - Might not compile if enabling nullable for the unit-test-project.
  • DbConnectionFactoryThrowsTest - This is a good test.
  • DbConnectionFactoryWithParametersWriteTest - Like this test as it combines both dbConnection-delegate and dbParameter-delegate.

I think these tests are already covered by existing test:

  • DbConnectionFactoryKeepConnectionTest
  • DbConnectionFactoryBatchedWriteTest
  • DbConnectionFactorySimpleWriteTest

@JohnVerheij
Copy link
Copy Markdown
Contributor Author

The 7 tests cover the DatabaseTarget(Func<IDbConnection>) constructor added in NLog/NLog#5878. Before this PR the user-provided factory variant has no direct tests; the existing suite covers the connection-string constructor only.

The 4 you want to keep cover defensive cases (null factory, factory-returns-null, factory-throws) and parameters-write. The 3 you want to drop (Simple, Batched, KeepConnection) give the factory constructor parity with the write-mode coverage the existing tests provide for the connection-string constructor.

Architecturally both constructors set _dbConnectionFactory and the single OpenConnection method invokes that field for every write (single and batched, KeepConnection on or off). There is no downstream branching by constructor. Under that invariant the contested 3 are redundant; they serve as regression coverage at the public API in case a future refactor introduces divergence. Will drop if you'd rather rely on the architecture invariant alone.

@snakefoot
Copy link
Copy Markdown
Contributor

snakefoot commented May 7, 2026

I don't see a need to future proof the unit-tests to handle any possible combination of code-changes in the future.

Hopefully any future changes to the code will include their own unit-tests. The goal for me is to verify that input-delegate is called when opening database-connection.

@JohnVerheij
Copy link
Copy Markdown
Contributor Author

Agreed. Narrowing to the 4 you listed. Will drop the other 3 and push an update shortly.

@JohnVerheij JohnVerheij force-pushed the tests/databasetarget-factory-ctor-coverage branch from 98ab414 to 70dea6c Compare May 13, 2026 16:42
@snakefoot
Copy link
Copy Markdown
Contributor

Closing and opening pull-request to tickle the build-server

@snakefoot snakefoot closed this May 14, 2026
@snakefoot snakefoot reopened this May 14, 2026
@snakefoot snakefoot merged commit 281a4ba into NLog:master May 14, 2026
3 checks passed
@snakefoot
Copy link
Copy Markdown
Contributor

Thank you for the nice contribution, and making the last adjustments. Much appreciated.

@JohnVerheij JohnVerheij deleted the tests/databasetarget-factory-ctor-coverage branch May 14, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants